package com.fan;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.List;
import java.io.InputStream;
import java.util.*;
import java.util.Timer;

import static com.fan.MyJFrame.wordsArrayList;


/**
 * @description: 面板
 * @author: fan
 * @date: 2022/1/24 13:39
 * @version: 1.0
 */
public class MyJPanel extends JPanel {

    private Image image = null;

    private Words words;

    public static int SCORE = 0;

    public static ArrayList<LotusLeaf> LOTUS_LEAF_LIST = new ArrayList<LotusLeaf>();

    public MyJPanel(Words words) {
        this.setLayout(null);

        try {
            // 读取背景图
            InputStream inputStream = MyJPanel.class.getClassLoader().getResourceAsStream("image/background.png");
            image = ImageIO.read(inputStream);

        } catch (Exception e) {
            e.printStackTrace();
        }

        this.words = words;
       action();

    }

    @Override
    protected void paintComponent(Graphics g) {
        // 把背景图画到画板上
        super.paintComponent(g);
        g.drawImage(image, 0, 0, EnglishWordsGameMain.SCREEN_WIDTH, EnglishWordsGameMain.SCREEN_HEIGHT, this);
        g.setColor(new Color(255,0,0));
        //画目标单词
        Font f = new Font("黑体 常规",Font.CENTER_BASELINE,20);
        g.setFont(f);
        //画分
        g.drawString("SCORE: " + SCORE, 10, 55);
        //画命
        g.drawString("LIFE: " + 5, 10, 75);

        //画目标单词
        f = new Font("黑体 常规",Font.BOLD,50);
        g.setFont(f);
        g.setColor(new Color(255 ,255 ,255));
        g.drawString(words.getChinese(),EnglishWordsGameMain.SCREEN_WIDTH/2-400,100);
    }


    private void action(){
        Timer timer = new Timer();
        int intervel = 10;
        timer.schedule(new TimerTask() {
            public void run() {
                enterAction();
                outOfBoundsAction();
                bulletBangAction();
                repaint();
            }
        }, intervel, intervel); //定时计划
    }

    /**
     * 添加荷叶
     */
    int enterIndex = 0;
    private void enterAction(){
        enterIndex++; //每10毫秒增1
        //每400(40*10)毫秒走一次
        if(enterIndex%100==0) {
            if(LOTUS_LEAF_LIST.size()< EnglishWordsGameMain.TOTAL){
                LotusLeaf lotusLeaf = nextOne();
                LOTUS_LEAF_LIST.add(lotusLeaf);
                this.add(lotusLeaf);
            }
        }
    }

    Random random = new Random();
    public static Map<Integer,Integer>  map = new HashMap();

    private int getIndex(){
        int flag = random.nextInt(wordsArrayList.size());
        if(map.get(flag) != null){
            flag =  getIndex();
        }
        map.put(flag,flag);
        return flag;
    }

    private LotusLeaf nextOne(){
        return new LotusLeaf(wordsArrayList.get( getIndex()));
    }


    /**
     * 把走到最左边的称到最右边去
     */
    private void outOfBoundsAction() {
        LOTUS_LEAF_LIST.forEach(lotusLeaf -> {
            int x = (int)(lotusLeaf.getLocation().getX()-1);
            if(x <= - lotusLeaf.getImage().getWidth(lotusLeaf)){
                x = EnglishWordsGameMain.SCREEN_WIDTH;
            }
            lotusLeaf.setLocation(x,(int)lotusLeaf.getLocation().getY());
        });
    }
    /** 删除已选正确的荷叶 */
    public void bulletBangAction() { //每10毫秒走一次
        Iterator<LotusLeaf> iterator = LOTUS_LEAF_LIST.iterator();
        while (iterator.hasNext()){
            LotusLeaf next = iterator.next();
            if(!next.isLife()){
                iterator.remove();
                this.remove(next);
            }
        }
    }

    public Words getWords() {
        return words;
    }

    public void setWords(Words words) {
        this.words = words;
    }
}
